package com.OnTimeTeam.OnTime;

import java.util.Calendar;

import android.content.Context;

public class SetQuickNap {


	public static AlarmFeatures setQuickNap(int interval, Context appContext){
		
		Calendar time = Calendar.getInstance();
		time.add(Calendar.MINUTE, interval);
		
		AlarmFeatures af = new AlarmFeatures();
		af.setStatus("On");
		af.setSortOfAlarm("Quick");
		af.setTune("0");
		af.setVibration(DefaultsIO.getDefaults(appContext.getFilesDir().getPath().toString() + "/defaults.xml").getVibration());
		af.setSnoozeRepeat("1");
		af.setLabel("Quick Nap Alarm");
		af.setVolume("100");
		af.setHour(Integer.toString(time.get(Calendar.HOUR_OF_DAY)));
		af.setMinute(Integer.toString(time.get(Calendar.MINUTE)));
		af.setIndex("0");
		af.setID(Integer.toString(AlarmSetter.createRandomAlarmID()));
		af.setDaysIDS(Integer.toString(AlarmSetter.createRandomAlarmID()));
		af.setQuickInterval(Integer.toString(interval));
		
		
		switch(time.get(Calendar.DAY_OF_WEEK)){
		case Calendar.SUNDAY:
			af.setDaysToRepeat("Sun");
			break;
		case Calendar.MONDAY:
			af.setDaysToRepeat("Mon");
			break;	
		case Calendar.TUESDAY:
			af.setDaysToRepeat("Tue");
			break;
		case Calendar.WEDNESDAY:
			af.setDaysToRepeat("Wed");
			break;	
		case Calendar.THURSDAY:
			af.setDaysToRepeat("Thu");
			break;
		case Calendar.FRIDAY:
			af.setDaysToRepeat("Fri");
			break;	
		case Calendar.SATURDAY:
			af.setDaysToRepeat("Sat");
			break;			
		}
		
		//set the alarm via AlarmManager
		AlarmSetter.setBackAlarm(af, appContext, appContext);
		
		//write the alarm to file for later use
		RemindersIO.writeNewRegularAlarmToFile(af, appContext.getFilesDir().getPath().toString() + "/reminders.xml");
	
		return af;
	}
	
	public static AlarmFeatures updateWithNewTime(AlarmFeatures af, int interval, Context appContext, int index) {
		
		Calendar time = Calendar.getInstance();
		time.add(Calendar.MINUTE, interval);
		
		af.setStatus("On");
		af.setHour(Integer.toString(time.get(Calendar.HOUR_OF_DAY)));
		af.setMinute(Integer.toString(time.get(Calendar.MINUTE)));
		af.setQuickInterval(Integer.toString(interval));
		
		switch(time.get(Calendar.DAY_OF_WEEK)){
			case Calendar.SUNDAY:
				af.setDaysToRepeat("Sun");
				break;
			case Calendar.MONDAY:
				af.setDaysToRepeat("Mon");
				break;	
			case Calendar.TUESDAY:
				af.setDaysToRepeat("Tue");
				break;
			case Calendar.WEDNESDAY:
				af.setDaysToRepeat("Wed");
				break;	
			case Calendar.THURSDAY:
				af.setDaysToRepeat("Thu");
				break;
			case Calendar.FRIDAY:
				af.setDaysToRepeat("Fri");
				break;	
			case Calendar.SATURDAY:
				af.setDaysToRepeat("Sat");
				break;			
		}
		
		String path = appContext.getFilesDir().getPath().toString() + "/reminders.xml";
		
		//write the new alarm to file and set the alarm via AlarmManager
		AlarmSetter.cancelExistingAlarm(af, appContext);
		AlarmSetter.setBackAlarm(af, appContext, appContext);
		RemindersIO.changeTimeAndSetAlarmOn(path, Integer.toString(index), af.getHour(), af.getMinute());
		
		return af;
	}
	
	
	public static AlarmFeatures updateTime(AlarmFeatures af, Context appContext, int index) {
		
		Calendar time = Calendar.getInstance();
		time.add(Calendar.MINUTE, Integer.valueOf(af.getQuickInterval()));
		
		af.setHour(Integer.toString(time.get(Calendar.HOUR_OF_DAY)));
		af.setMinute(Integer.toString(time.get(Calendar.MINUTE)));
		af.setStatus("On");
		
		switch(time.get(Calendar.DAY_OF_WEEK)){
			case Calendar.SUNDAY:
				af.setDaysToRepeat("Sun");
				break;
			case Calendar.MONDAY:
				af.setDaysToRepeat("Mon");
				break;	
			case Calendar.TUESDAY:
				af.setDaysToRepeat("Tue");
				break;
			case Calendar.WEDNESDAY:
				af.setDaysToRepeat("Wed");
				break;	
			case Calendar.THURSDAY:
				af.setDaysToRepeat("Thu");
				break;
			case Calendar.FRIDAY:
				af.setDaysToRepeat("Fri");
				break;	
			case Calendar.SATURDAY:
				af.setDaysToRepeat("Sat");
				break;			
		}
		
		String path = appContext.getFilesDir().getPath().toString() + "/reminders.xml";
		
		//write the new alarm to file and set the alarm via AlarmManager
		AlarmSetter.cancelExistingAlarm(af, appContext);
		AlarmSetter.setBackAlarm(af, appContext, appContext);
		RemindersIO.changeTimeAndSetAlarmOn(path, Integer.toString(index), af.getHour(), af.getMinute());
		
		return af;
	}
	
	
}
